import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gameplay_flutter/lib_basemodel/home/room_list_model.dart';
import 'package:gameplay_flutter/lib_network/http_api.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'dart:ui' as ui;

import '../../../lib_basemodel/chat_room/kh_chat_room_enter_model.dart';
import '../../../lib_basemodel/chat_room/kh_chat_room_info_model.dart';
import '../../../lib_basemodel/country/country_model.dart';
import '../../../lib_network/http_helper.dart';
import '../../../m_chatroom/src/interface/room_page.dart';
import '../../../resource/language/generated/l10n.dart';


class HomeDataType {
  static const room = 1;
  static const user = 2;
}

class HomeCountriesController extends GetxController with GetTickerProviderStateMixin {
  final recommend = <Recommend>[].obs;
  late Background? background;
  final countries = <CountryModel>[].obs;
  final box = GetStorage();
  String? errorMsg;
  // late List<GlobalKey<HomeSubPageState>> subPageKeys;

  @override
  void onInit() {
    super.onInit();
    countries.value = box.read('countryList');
  }

  @override
  void onReady() {
    super.onReady();
  }

  joinRoomPage(BuildContext context,model){
    ///进入房间
    print('首页跳转');
    // joinRoomPage
    /// 进入房间要用的模型
    KHChatRoomEnterModel enterModel = KHChatRoomEnterModel();
    enterModel.enterRoomNo = model.roomNo;
    if (model.rangeTypeStr != null) {
      enterModel.rangeType = int.parse(model.rangeTypeStr!);
    }
    enterModel.template = model.roomTemplateId;

    /// 基础数据
    KHChatRoomInfoModel roomInfoModel = KHChatRoomInfoModel();

    roomInfoModel.roomCategoryId = model.roomCategoryId;
    roomInfoModel.icon = model.roomIcon;
    roomInfoModel.roomNo = model.roomNo;
    roomInfoModel.name = model.roomName;
    roomInfoModel.name = model.roomName;
    roomInfoModel.roomTemplateId = model.roomTemplateId;
    roomInfoModel.roomCode = model.roomCode;

    enterModel.roomBaseInfoModel = roomInfoModel;
    if (model.roomNo != null) {
      RoomPage.show(context, int.parse(model.roomNo!).toString());
    }
  }

  Future loadData(String countries,type,pageNum) async {
    print('执行查询');
    var resp = await HttpHelper.instance().post(
        HttpApi.appGetCommend,
        showLoading: true,
        showToast: true,
        body:{
          'pageNum':pageNum,
          'pageSize':20,
          'country': countries
        }
    );
    if (resp.isSuccess && resp.result != null) {
      var data = RoomListModel.fromJson(resp.result);
      if(data.recommend!.isNotEmpty){
        if(type == 'more'){
          List<Recommend> list = recommend.value;
          for(var item in data.recommend!){
            list.add(item);
          }
          recommend.value = list;
        } else {
          recommend.value = data.recommend!;
        }
        update();
        return true;
      } else {
        if(type != 'more'){
          recommend.value = [];
        }
        update();
        return false;
      }

    } else {
      errorMsg = resp.result?['description'];
      return false;
    }
  }

  @override
  void onClose() {
    super.onClose();
  }


}
